home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: Empty String function
- Date: Fri, 15 Mar 96 14:50:46 GMT
- Organization: none
- Message-ID: <826901446snz@genesis.demon.co.uk>
- References: <1996Mar14.205741.19178@vtf.idx.com>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <1996Mar14.205741.19178@vtf.idx.com>
- sjjm@hawkeye.idx.com "Steve Mount" writes:
-
- >int zemp(char *ptxt, int len)
- >{
- >static int i;
-
- Why make i static? It would be better off as an auto variable.
-
- >if (len == 0) len = strlen(ptxt); // Allow null-term strings
-
- If you are writing C code // is illegal syntax - C only supports /* */
- comments. A conforming compiler is required to diagnose //
-
- >i = strspn(ptxt," \n\t");
- >if (i >= len) return(-1); // -1 == EMPTY
- >return(i);
- >}
- >
- >
- >The problem is when the buffer is not null-terminated, which
- >happens a lot. The strspn function COULD read past the end
- >of the buffer.... potentially WAY past it. Possibly even into
- >memory not owned by the program.
-
- Right, you *must* pass strspn() a pointer to null character terminated string
- or else the call results in undefined behaviour i.e. anything can happen.
- There's nothing to stop strspn() calling, say, strlen() internally.
-
- >This is where I get concerned.
-
- With good reason!
-
- >Anyone know if this could cause trouble, generally, and if
- >it could cause trouble specifically in DOS/Win/Unix (it is
- >used in a Unix app now).
-
- It will almost certainly cause trouble sooner or later in just about any
- environment. It could easily trap in a protected enviroment if it walks
- outside the permitted address space. It could also become very slow.
-
- You need to ensure that you are passing a valid string to strspn(). Perhaps
- you can ensure that the buffer has space to add a terminating null character
- or else you would have to replace the last character with a null character
- before the search and restore it afterwards. If the rest of the buffer
- was blank you would have to check that character explicitly. Something
- like:
-
- int zemp(char *ptxt, int len)
-
- {
- int i;
- char ch;
-
- if (len == 0) {
- len = strlen(ptxt);
- i = strspn(ptxt, " \n\t");
-
- if (i == len)
- return -1;
- } else {
- len--;
- ch = ptxt[len];
- ptxt[len] = '\0';
- i = strspn(ptxt, " \n\t");
- ptxt[len] = ch;
-
- if (i == len && (ch == ' ' || ch == '\n' || ch == '\t'))
- return -1;
- }
-
- return i;
- }
-
- You whould have to be careful not to pass a pointer to non-modifiable data
- (e.g. a string literal) if len is non-zero. The strspn() approach here
- is a bit messy - what other methods have you tried?
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-